<?php
class Mwc_SystemPoint_MembershipController extends Mage_Adminhtml_Controller_Action{
    public function indexAction(){
        $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('systempoint/membership_list'))
            ->renderLayout();
    }

    public function deleteAction(){
        $userpointId = $this->getRequest()->getParam('id', false);

        try {
            Mage::getModel('systempoint/membership')->setId($userpointId)->delete();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('systempoint')->__('Membership successfully deleted'));
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));

            return;
        }
        catch (Exception $e){
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }

        $this->_redirectReferer();
    }

    public function newAction(){
        $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('systempoint/membership_new'))
            ->renderLayout();
    }

    public function postAction(){
        if ($data = $this->getRequest()->getPost()){
            $membership = Mage::getModel('systempoint/membership')->setData($data);
            // Save photo
		     if(isset($_FILES['icon_path']['name']) && $_FILES['icon_path']['name'] != '') {
		         try {    
		             $uploader = new Varien_File_Uploader('icon_path');
		
		             $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
		             $uploader->setAllowRenameFiles(false);
		             $uploader->setFilesDispersion(false);
		
		             // Set media as the upload dir
		             $media_path  = Mage::getBaseDir('media') . DS;         
		
		             // Upload the image
		             $uploader->save($media_path, $_FILES['icon_path']['name']);
		
		             $data['icon_path'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . $_FILES['icon_path']['name']; 
		         } 
		         catch (Exception $e) {
		             print_r($e);
		             die;
		         }     
		         }
		     else {       
		         if(isset($data['icon_path']['delete']) && $data['icon_path']['delete'] == 1) {
		             $data['icon_path'] = '';
		         }
		         else {
		             unset($data['icon_path']);
		         }
		      }
		     if(isset($data['icon_path'])) $membership->setData('icon_path',$data['icon_path']); 
		              
            try{
                $membership->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('systempoint')->__('Membership was successfully saved'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));

                return;
            }
            catch (Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->getResponse()->setRedirect($this->getUrl('*/*/'));

        return;
    }

    public function editAction(){
        $this->loadLayout();
        $this->_addContent($this->getLayout()->createBlock('systempoint/membership_edit'));
        $this->renderLayout();
    }

    public function saveAction(){
        $id = $this->getRequest()->getParam('id', false);
        if ($data = $this->getRequest()->getPost()) {
            $membership = Mage::getModel('systempoint/membership')->load($id)->addData($data);
            // Save photo
		     if(isset($_FILES['icon_path']['name']) && $_FILES['icon_path']['name'] != '') {
		         try {    
		             $uploader = new Varien_File_Uploader('icon_path');
		
		             $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
		             $uploader->setAllowRenameFiles(false);
		             $uploader->setFilesDispersion(false);
		
		             // Set media as the upload dir
		             $media_path  = Mage::getBaseDir('media') . DS;                
		
		             // Upload the image
		             $uploader->save($media_path, $_FILES['icon_path']['name']);
		
		             $data['icon_path'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . $_FILES['icon_path']['name'];
		
		         } 
		         catch (Exception $e) {
		             print_r($e);
		             die;
		         }     
		         }
		     else {       
		         if(isset($data['icon_path']['delete']) && $data['icon_path']['delete'] == 1) {
		             $data['icon_path'] = '';
		         }
		         else {
		             unset($data['icon_path']);
		         }
		      }
		     if(isset($data['icon_path'])) $membership->setData('icon_path',$data['icon_path']);
		        
            try {
                $membership->setId($id)->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('systempoint')->__('Membership successfully updated'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));

                return;
            }
            catch (Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }

        $this->_redirectReferer();
    }
}